package com.yanqu.road.server.cmd.activity.dafuweng;

import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.DaFuWengActivity;
import com.yanqu.road.server.manager.cross.battle.DaFuWengMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.C_CROSS_DA_FU_WENG_PRE_HANDLE_BOSS_EVENT, desc = "跨服预处理boss事件")
public class CrossPreHandleBossEventCmd extends CrossUnionCommand {

    private int activityId;
    private long serverId;
    private long userId;
    private MonopolyProto.RollDiceRespMsg rollMsg;
    private String unionUid = "";

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MonopolyProto.CrossPreHandleBossEventReqMsg reqMsg = MonopolyProto.CrossPreHandleBossEventReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        serverId = reqMsg.getServerId();
        userId = reqMsg.getUserId();
        rollMsg = reqMsg.getRollMsg();
        if (reqMsg.hasUnionUid()) {
            unionUid = reqMsg.getUnionUid();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        DaFuWengActivity daFuWengActivity = DaFuWengMgr.getDaFuWengActivity(activityId, groupId);
        if (daFuWengActivity == null) {
            return;
        }
        int bossId = daFuWengActivity.getRandomAliveBossId();

        // 添加商会情报
        int addNewsResult = 0;
        if (!StringUtils.isNullOrEmpty(unionUid)) {
            addNewsResult = daFuWengActivity.addUnionNews(serverId, userId, unionUid, bossId);
        }

        MonopolyProto.CrossPreHandleBossEventRespMsg.Builder builder = MonopolyProto.CrossPreHandleBossEventRespMsg.newBuilder();
        builder.setUserId(userId);
        builder.setBossId(bossId);
        builder.setAddNewsResult(addNewsResult);
        builder.setActivityId(activityId);
        builder.setRollMsg(rollMsg);
        YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_DA_FU_WENG_GET_RANDOM_BOSS_FROM_CROSS, builder);
        MessageHelper.sendPacket(serverId, userId, yanQuMessage);
    }

    @Override
    public void sendData() {

    }
}
